<?php

namespace App\Console\Commands;

use App\RiskOrder;
use App\RiskRule;
use App\Services\RuleService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CheckOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:check-order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查订单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        //处理有百融数据，订单详情状态数据
        $list = DB::table('ims_ewei_shop_order_detail')->where('id','>',10930)
            ->where('is_risk',0)
            ->whereNotNull('bairong_report')
            ->update(['is_risk'=>1]);



        $list = DB::table('ims_ewei_shop_order_detail')->where('is_risk',1)->limit(40)->get();
        foreach ($list as $item){
            $order = DB::table('ims_ewei_shop_order as o')
                ->leftJoin('ims_ewei_shop_member as m', 'o.openid', '=', 'm.openid')
                ->where('o.id',$item->orderid)
                ->select('o.id','o.openid','o.ordersn','o.createtime','o.total_receivable','m.realname','m.id_card','m.mobile')
                ->first();
            if(empty($item->bairong_report)){
                $status = 1;
            }else{
                $status = 2;
            }

            $risk_order = RiskOrder::where('orderid',$order->id)->first();
            if(!$risk_order){
                $risk_order = new RiskOrder();
            }
            $risk_order->orderid = $order->id;
            $risk_order->ordersn = $order->ordersn;
            $risk_order->id_name = $order->realname;
            $risk_order->id_card = $order->id_card;
            $risk_order->mobile = $order->mobile;
            $risk_order->openid = $order->openid;
            $risk_order->money = $order->total_receivable;
            $risk_order->status = $status;
            $risk_order->data = $item->bairong_report;
            $risk_order->data_ali = $item->ali_report;
            $risk_order->xaidan_at = date('Y-m-d H:i:s',$order->createtime);
            $risk_order->save();
            DB::table('ims_ewei_shop_order_detail')->where('id',$item->id)->update(['is_risk'=>2]);
        }

        $this->checkOrder();


    }

    public function checkOrder()
    {
        $list = RiskOrder::where('status',2)->limit(200)->get();
        $rule_service = new RuleService();
        foreach ($list as $val){
            var_dump($val->orderid);
            $rule_service->updateLevel($val);
        }






    }


}
